package threadpool;

import thread.MyRunnable;

import java.util.List;
import java.util.concurrent.*;

public class DemoThreadPool {
    public static void main1(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 4, 8, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1), new ThreadPoolExecutor.AbortPolicy());
        MyRunnable runnable1 = new MyRunnable();
        MyRunnable runnable2 = new MyRunnable();
        MyRunnable runnable3 = new MyRunnable();
        MyRunnable runnable4 = new MyRunnable();
        MyRunnable runnable5 = new MyRunnable();
        MyRunnable runnable6 = new MyRunnable();
        executor.execute(runnable1);
        executor.execute(runnable2);
        executor.execute(runnable3);
        executor.execute(runnable4);
        executor.execute(runnable5);
        executor.execute(runnable6);
        List<Runnable> list = executor.shutdownNow();
        for (Runnable tmp : list) {
            System.out.println("还未执行的线程" + tmp);
        }
        ExecutorService service = Executors.newCachedThreadPool();

    }

    public static void main2(String[] args) {
        // 创建固定核心数的线程池,也为最大线程数
        ExecutorService service1 = Executors.newFixedThreadPool(3);
        service1.execute(() ->
                {
                    int x = 20;
                    while (x-- > 0) {
                        System.out.println(Thread.currentThread().getName());
                        throw new RuntimeException();
                    }
                }
        );
        service1.execute(() ->
                {
                    int x = 1;
                    while (x-- > 0) {
                        System.out.println(Thread.currentThread().getName());
                    }
                }
        );
        service1.execute(() ->
                {
                    int x = 40;
                    while (x-- > 0) {
                        System.out.println(Thread.currentThread().getName());
                    }
                }
        );
        service1.execute(() ->
                {
                    int x = 1;
                    while (x-- > 0) {
                        System.out.println(Thread.currentThread().getName());
                    }
                }
        );
    }

    public static void main(String[] args) {
        // 随着任务数量的增加线程数量增加
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(() -> {
            {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName());
            }
        });
        service.execute(() -> {
            {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName());
            }
        });
        service.execute(() -> {
            {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName());
            }
        });
        service.execute(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName());

        });
        service.execute(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName());
        });

        service.execute(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName());
        });
        System.out.println(((ThreadPoolExecutor) service).getPoolSize() + "++++++++++++++++++++++++++++++");
    }
}
